<?php

require_once 'db.php';
require_once 'config/config.php.temp';

class works extends db {

    public $start;
    public $page;

    public function select() {
        $this->connect();
        $query = "SELECT * FROM works LIMIT $this->start, $this->page";
        $works = $this->query($query);
        return $works;
    }

    public function count() {
        $this->connect();
        $query = "SELECT * FROM works";
        $works = $this->query($query);
        return count($works);
    }

    function edit($edit = NULL) {
        $this->connect();
        $query = "insert into works (image) values('" . $edit["image"] . "')";
        $result = mysql_query($query);
        if ($result)
            return "";
        else {
            return mysql_query($query);
        }
    }

    public function select_edit($page = NULL, $id = NULL) {
        $this->connect();
        $where = "";
        if (!empty($id))
            $where = "where id=$id";
        $limit = "";
        if (!empty($page))
            $limit = 'limit ' . $begin . ',' . PER_PAGE;
        $begin = ($page - 1) * PER_PAGE;
        $query = "SELECT * FROM works $where $limit";
        $result = mysql_query($query);
        $result = mysql_fetch_assoc($result);
        return $result;
    }
    
    public function update($array = null) {
        $query = "Update works set name='" . $array['name'] .
                 "', description='" . $array['description'] .
                 "', image='" . $array['image'].
                 "' where id=" . $array['id'];
        mysql_query($query);
        if (mysql_query($query)) {}
        else
            echo "fail";
    }
    
    public function update1($array = null) {
        $query = "Update works set name='" . $array['name'] .
                 "', description='" . $array['description'] .
                 "' where id=" . $array['id'];
        mysql_query($query);
        if (mysql_query($query)) {}
        else
            echo "fail";
    }

}

?>